<template>
  <div class="page-mobile-course__my">
    <div class="no-data" v-if="course_record_list.length == 0">
      <van-empty :description="$t('locale.mobile.courseMy.t1')" />
    </div>
    <course-card
      v-for="(item, index) in course_my_list"
      :detail="item"
      :memberDiscount="memberDiscount"
      :type="'bought'"
      :key="index"
    ></course-card>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
import { getTest } from "~/api/test";
import { getOrderList, getMyOrderList,getOrder } from "~/api/order";
import { getMemberInfo,getMember } from "~/api/member";
import CourseCard from "~/components/mobile/CourseCard.vue";
export default {
  components: {
    CourseCard
  },
  data() {
    return {
      course_obj: {},
      course_my_list:[],
      memberDiscount:{}
    };
  },
  computed: {
    ...mapState({
      user_info(state) {
        return state.user_info;
      },
      major_list(state) {
        return state.major_list;
      },
      course_record_list(state) {
        return state.course_record_list;
      },
      token(state) {
        return state.token;
      }
    }),
    // //我的课程
    // course_list() {
    //   const { page, pageSize } = this.course;
    //   return this.course_my_list.slice(
    //     (page - 1) * pageSize,
    //     page * pageSize
    //   );
    // },
  },
  activated() {
    if (process.client) {
      if (!this.token && Object.keys(this.user_info).length == 0) {
        this.$router.replace("/m/login");
        return;
      }
    }
    this.initBuyCourseRecord();
    this.getMyCoursesMine();
  },
  watch: {},
  methods: {
    ...mapActions(["initMajorList", "initBuyCourseRecord"]),
    ...mapMutations(["SET_IS_LOADING"]),
    async getMemberInfo(){
      this.SET_IS_LOADING(true);
      if(this.token && Object.keys(this.user_info).length > 0){
        let memberDiscount = await getMemberInfo(this.user_info.id);
        if (memberDiscount && memberDiscount.data && memberDiscount.data.code == 200) {
          this.memberDiscount = memberDiscount.data.data;
        } else {
          
        }
      }
      this.SET_IS_LOADING(false);
    },
    async getMyCoursesMine() {
      this.SET_IS_LOADING(true);
      let params = {
        userId: this.user_info.id,
        sort:"id desc"
      };
      let result = await getMyOrderList(params);
      let result1 = await getOrder(params);
      console.log("result:", result);
      console.log("result1:", result1.data.data.rows);
      if (result && result.data && result.data.code == 200 && result1 && result1.data && result1.data.code == 200) {
        let courseArr = [];
        for(let i = 0;i<result.data.data.rows.length;i++){
          for(let j = 0;j<result1.data.data.rows.length;j++){
            if(result.data.data.rows[i].id === result1.data.data.rows[j].courseId){
              courseArr.push({...result.data.data.rows[i],deleted:result1.data.data.rows[j].deleted})
            }
          }
        }
        console.log("courseArr",courseArr)
        
        this.course_my_list = courseArr;


        // console.log(result.data.data.rows);
        // this.underway_course_list = result.data.data.rows.filter(
        //   item => item.buyCourseDeadlineDate >= new Date().getTime()
        // );
        // console.log(this.underway_course_list);
        // this.SET_COURSE_RECORD_LIST(result.data.data.rows);
        // this.getTestPapers(result.data.data.rows);


        // this.course_my_list = result.data.data.rows;
      } else {
        this.$Message.error("获取失败！");
      }
      this.SET_IS_LOADING(false);
    },
    async getData() {
      // return new Promise(async (res, rej) => {
      //   this.SET_IS_LOADING(true);
      //   let result = await getTest({
      //     id: this.courseId
      //   });
      //   if (result && result.data && result.data.code == 200) {
      //     const { data } = result.data;
      //     if (data && data.rows && data.rows.length > 0) {
      //       this.course_obj = data.rows[0];
      //     }
      //   } else {
      //     this.$toast(("加载失败！", result.data.msg));
      //   }
      //   this.SET_IS_LOADING(false);
      //   res();
      // }).then();
    },
    getMajorName(item) {
      let res = this.major_list.filter(i => i.id == item.majorId);
      if (res.length > 0) {
        return res[0].name;
      } else {
        return "未知";
      }
    }
  },
  created() {
    // console.log("                                                   \n"+       
    //             " __                       .__                      \n"+  
    //             "|  | __   ____     ____   |  |     ____   _____    \n"+  
    //             "|  |/ / _/ __ \\  _/ __ \\  |  |   _/ __ \\  \\__  \\   \n"+  
    //             "|    <  \\  ___/  \\  ___/  |  |__ \\  ___/   / __ \\_ \n"+  
    //             "|__|_ \\  \\___  >  \\___  > |____/  \\___  > (____  / \n"+  
    //             "     \\/      \\/       \\/              \\/       \\/   \n");
    if (process.client) {
      if (!this.token && Object.keys(this.user_info).length == 0) {
        this.$router.replace("/m/login");
        return;
      }
    }
    this.getData();
    this.initMajorList();
    this.getMemberInfo();
  },
  mounted() {}
};
</script>

<style lang="scss" scoped>
.page-mobile-course__my {
  width: 6.8rem;
  // margin: 0 auto;
}
</style>
<style lang="scss">
.page-mobile-course__my {
}
</style>
